﻿// Module:             Games Programming
// Assignment1:        Platformer Game
// Add game feature:   
// Student Number:     S10095741D
// Student Name:       Ng Cheong Sian

//////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Game;

namespace Game.UIScripts
{
    class SplashScreen : Panel
    {
        private readonly ContentManager _contentManager;
        private readonly Texture2D[] _logo;
        private int _counter;

        private readonly UIController _ui;

        private readonly int _viewportWidth;
        private readonly int _viewportHeight;
        int _alphaValue = 1;
        int _fadeIncrement = 10;
        double _fadeDelay = 0;

        public SplashScreen(IServiceProvider serviceProvider, GraphicsDevice GraphicsDevice, ContentManager content, UIController controller)
        {
            _contentManager = content;
            _ui = controller;
            _viewportWidth = GraphicsDevice.Viewport.Width;
            _viewportHeight = GraphicsDevice.Viewport.Height;
            _logo = new Texture2D[2];
            LoadContent();
        }


        private void LoadContent()
        {
            _logo[0] = _contentManager.Load<Texture2D>("Sprites/UI/SplashScreen/NP_Logo");
            _logo[1] = _contentManager.Load<Texture2D>("Sprites/UI/SplashScreen/ICT_Logo");
        }

        public void Reset()
        {
            _alphaValue = 1;
            _fadeDelay = 0;
            _counter = 0;
        }


        public void Update(GameTime gameTime, KeyboardState keyboardState, MouseState mouseState)
        {
            /*if (Keyboard.GetState().IsKeyDown(Keys.Space))
                _ui.ChangePanel(UIController.PanelType.GameScreen, this);
            if (Keyboard.GetState().IsKeyDown(Keys.Z))
                _ui.ChangePanel(UIController.PanelType.MainMenu, this);*/
            _fadeDelay -= gameTime.ElapsedGameTime.TotalSeconds;

            if (_fadeDelay <= 0)
            {
                _alphaValue += _fadeIncrement;

                if (_alphaValue >= 255)
                {
                    _fadeDelay = 1.5;
                    _fadeIncrement *= -1;
                }
                else if(_alphaValue <= 0)
                {
                    _fadeDelay = 0.5;
                    _fadeIncrement *= -1;
                    _counter++;
                }
            }
        }


        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();

            if (_counter != _logo.Length) 
                spriteBatch.Draw(_logo[_counter], new Vector2(x: (_viewportWidth - _logo[_counter].Width) / 2, y: (_viewportHeight - _logo[_counter].Height) / 2), new Color(255, 255, 255, _alphaValue));
            else
                _ui.ChangePanel(UIController.PanelType.MainMenu,this);
            spriteBatch.End();
        }

    }
}
